apply plugin: 'com.android.application'

import java.nio.file.Paths

def projectRoot         = new File('./../..').getCanonicalPath()
def rhoRoot             = '<%= @rhoRoot %>'
def rhoBuildDir         = Paths.get( projectRoot, 'bin/tmp' ).toString()
def rhoOutDir           = Paths.get( projectRoot, 'bin/target/android' ).toString()
def rhoManifest         = Paths.get( rhoBuildDir, 'AndroidManifest.xml' ).toString()
def rhoRes              = Paths.get( rhoBuildDir, 'res' ).toString()
def rhoAssets           = Paths.get( rhoBuildDir, 'assets' ).toString()
def rhoGen              = Paths.get( rhoBuildDir, 'gen' ).toString()
def rhoJniLibs          = Paths.get( rhoBuildDir, 'lib' ).toString()

android {
    compileSdkVersion <%= @compileSdkVersion %>
    buildToolsVersion "<%= @buildToolsVersion %>"
    defaultConfig {
        applicationId "<%= @applicationId %>"
        minSdkVersion <%= @minSdkVersion %>
        targetSdkVersion <%= @targetSdkVersion %>
        versionCode <%= @versionCode %>
        versionName "<%= @versionName %>"
    }

    sourceSets {
        main {
            manifest.srcFile rhoManifest
            res.srcDirs = [ rhoRes ]
            assets.srcDirs = [ rhoAssets ]
            //jniLibs.srcDirs = [ rhoJniLibs ]

            java {
                srcDir Paths.get( rhoRoot, 'platform/android/Rhodes/src' ).toString()
                srcDir rhoGen

                include 'com/rhomobile/rhodes/R/R.java'
                exclude 'com/rhomobile/rhodes/mapview/AnnotationsOverlay.java'
                exclude 'com/rhomobile/rhodes/mapview/GoogleMapView.java'
                exclude 'com/rhomobile/rhodes/mapview/CalloutOverlay.java'
                exclude 'com/rhomobile/rhodes/alert/*'
                exclude 'android/support/v7/appcompat/R.java'
                exclude 'com/google/android/gms/R.java'
                exclude 'com/rhomobile/rhodes/R.java'
            }
        }
    }
	
	externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
	
	buildTypes {
        release {
            minifyEnabled false
            //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
	
    defaultConfig {
        ndk {
            abiFilters '<%= @targetArch %>'
        }
       externalNativeBuild {
            cmake {
                arguments '-DANDROID_TOOLCHAIN=gcc-4.9','-D_FORTIFY_SOURCE=0'
                cppFlags '-v','-fuse-ld=gold','-shared','-Wl,--no-whole-archive','-Wl,--no-undefined','-Wl,-z,defs','-fPIC','-lc','-lm'
            }
        }
    }
	
	packagingOptions {
        exclude 'jsr305_annotations/Jsr305_annotations.gwt.xml'
        exclude 'build-data.properties'
    }

}

import org.apache.tools.ant.taskdefs.condition.Os

task rhoBuild(type:Exec) {
    logger.info "Building RHO application"

    workingDir projectRoot

    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'cmd', '/c', 'rake'
    } else {
        commandLine 'rake'
    }

    args 'device:android:studio_build'

    standardOutput = new ByteArrayOutputStream()

    doLast {
        logger.info standardOutput.toString()
    }
}

rhoBuild.outputs.upToDateWhen { new File(rhoBuildDir).exists() }

//preBuild.dependsOn rhoBuild

tasks.whenTaskAdded { task ->
    logger.debug 'TASK ADDED: ' + task.toString();

    if ( task.name == 'lintVitalRelease' ) {
        logger.debug 'SKIPPING LING CHECKS TASK'
        task.outputs.upToDateWhen { true }
    } else if ( task.name =~ /compile.*Ndk/ ) {
        logger.debug 'SKIPPING NDK BUILD TASK'
        task.outputs.upToDateWhen { true }
    }

    /*
    if (task.name.contains("externalNativeBuildDebug"))
        externalNativeBuildDebug.finalizedBy(rhoBuild);
    else if(task.name.contains("externalNativeBuildRelease"))
        externalNativeBuildRelease.finalizedBy(rhoBuild);
    */

    if (task.name.contains("transformNativeLibsWithMergeJniLibsForDebug") ||
            task.name.contains("transformNativeLibsWithMergeJniLibsForRelease"))
    {
        logger.debug 'DISABLED: ' + task.toString();
        task.enabled = false;
    }
}

dependencies {
    implementation fileTree(include: ['**/*.jar'], exclude: ['**/Rhodes.jar'], dir: rhoOutDir )
    implementation fileTree(include: ['**/*.jar'], exclude: ['**/Rhodes.jar', '.m2/**/*'], dir: rhoBuildDir )
    compileOnly fileTree(include: ['**/*.jar'], dir: Paths.get(rhoRoot, 'platform/android/lib').toString() )

    implementation 'com.google.android.gms:play-services-vision:9.4.0'
    implementation 'com.android.support:appcompat-v7:25.2.0'

}